package com.ruoyi.jetLinks.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.WindUtil;
import com.ruoyi.common.utils.http.HttpClient4Utils;
import com.ruoyi.jetLinks.domain.NmEquipment;
import com.ruoyi.jetLinks.service.INmEquipmentService;
import com.ruoyi.jetLinks.util.QRCodeUtil;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/test")
public class TEST extends BaseController {
    @Autowired
    private INmEquipmentService nmEquipmentService;
    /**
     * 获取设备信息管理详细信息
     */
    @GetMapping("/queryList222")
    public AjaxResult getInfo2() throws Exception {
// 存放在二维码中的内容

        String text = "https://news.online.sh.cn/news/gb/content/2020-08/24/content_9629594.htm";
        // 嵌入二维码的图片路径
        String imgPath = "C:\\Users\\Lenovo\\Desktop\\tupian\\b4b8c3764d5a4999ccba8a63c6a6b84.jpg";
        // 生成的二维码的路径及名称
        String destPath = "D:/123.png";
        //生成二维码
      //  QRCodeUtil.encode(text, imgPath, destPath, true);
        // 解析二维码
        String str = QRCodeUtil.decode(destPath);
        // 打印出解析出的内容
        System.out.println(str);
        return AjaxResult.success();
    }

    @GetMapping("/queryList11")
    public AjaxResult getInfo() {
        InputStream inputStream = null;
        String str = null;
        Pattern p4 = Pattern.compile("addr=(.*?) tagname");
        Pattern p1 = Pattern.compile("<tr> (.*?) </tr>");
        try{
            Map<String,String> requestHeadMap = new HashMap<>();
            requestHeadMap.put("Host","www.yibiao120.com");
            requestHeadMap.put("Cookie","PHPSESSID=984oaddmkbr6vu16ee96dvbfd1; DedeUserID=15; DedeUserID__ckMd5=7df3292f2dc45da8; DedeLoginTime=1639709875; DedeLoginTime__ckMd5=0eedfb09eb875dd8");
            for (int  i=1;i<9;i++){
                String url = "http://www.yibiao120.com/plus/diy.php?action=listrq&diyid=2&totalresult=108&pageno="+i;

                //  requestHeadMap.put("Content-Type", "application/x-www-form-urlencoded");
                inputStream = HttpClient4Utils.sendGet(url,requestHeadMap);
                str = IOUtils.toString(inputStream, "utf-8");

                List<String> resultList = WindUtil.getContext(str,p4);
                for(int k=0;k<resultList.size();k++){
                    System.out.println("i="+i+"        k="+k);
                    String a = resultList.get(k);
                    String url1 = resultList.get(0).substring(6,a.length()-9);
                    inputStream = HttpClient4Utils.sendGet(url1,requestHeadMap);
                    str = IOUtils.toString(inputStream, "utf-8");
                    Document document = Jsoup.parse(str);
                    NmEquipment equipment = new NmEquipment();

                    Elements tableList = document.getElementsByTag("table");
                    for(int l=0;l<tableList.size();l++){
                        Elements tbtdList =  tableList.get(l).getElementsByTag("td");
                        for(int r=0;r<tbtdList.size();r=r+2){
                            if(l==0){
                                if("设备位号".equals(tbtdList.get(r).text())){
                                    equipment.setEquipmentNo(tbtdList.get(r+1).text());
                                }
                                if("出厂日期".equals(tbtdList.get(r).text())){
                                    equipment.setProductionDate(tbtdList.get(r+1).text());
                                }
                                if("数量".equals(tbtdList.get(r).text())){
                                    equipment.setNum(tbtdList.get(r+1).text());
                                }
                                if("设备名称".equals(tbtdList.get(r).text())){
                                    equipment.setEquipmentName(tbtdList.get(r+1).text());
                                }
                                if("安装单位".equals(tbtdList.get(r).text())){
                                    equipment.setInstallationUnit(tbtdList.get(r+1).text());
                                }
                                if("出厂编号".equals(tbtdList.get(r).text())){
                                    equipment.setProductionNo(tbtdList.get(r+1).text());
                                }

                                if("型号".equals(tbtdList.get(r).text())){
                                    equipment.setModel(tbtdList.get(r+1).text());
                                }

                                if("外形尺寸".equals(tbtdList.get(r).text())){
                                    equipment.setOverallDimension(tbtdList.get(r+1).text());
                                }
                                if("总重kg".equals(tbtdList.get(r).text())){
                                    equipment.setTotalWeight(tbtdList.get(r+1).text());
                                }
                                if("最大起吊重量kg".equals(tbtdList.get(r).text())){
                                    equipment.setLiftingWeightMax(tbtdList.get(r+1).text());
                                }
                                if("制造厂".equals(tbtdList.get(r).text())){
                                    equipment.setManufacturer(tbtdList.get(r+1).text());
                                }
                                if("投产日期".equals(tbtdList.get(r).text())){
                                    equipment.setProductionTime(tbtdList.get(r+1).text());
                                }
                            }
                            if(l==1){
                                if("介质".equals(tbtdList.get(r).text())){
                                    equipment.setMedium(tbtdList.get(r+1).text());
                                }
                                if("设计流量m3/h".equals(tbtdList.get(r).text())){
                                    equipment.setDesignFlow(tbtdList.get(r+1).text());
                                }
                                if("转速".equals(tbtdList.get(r).text())){
                                    equipment.setSpeed(tbtdList.get(r+1).text());
                                }
                                if("入口温度℃".equals(tbtdList.get(r).text())){
                                    equipment.setInletTemperature(tbtdList.get(r+1).text());
                                }
                                if("正常流量".equals(tbtdList.get(r).text())){
                                    equipment.setNormalFlow(tbtdList.get(r+1).text());
                                }
                                if("轴功率".equals(tbtdList.get(r).text())){
                                    equipment.setShaftPower(tbtdList.get(r+1).text());
                                }
                                if("粘度".equals(tbtdList.get(r).text())){
                                    equipment.setViscosity(tbtdList.get(r+1).text());
                                }
                                if("入口压力MPa".equals(tbtdList.get(r).text())){
                                    equipment.setInletPressure(tbtdList.get(r+1).text());
                                }
                                if("气蚀余量m".equals(tbtdList.get(r).text())){
                                    equipment.setCavitationAllowance(tbtdList.get(r+1).text());
                                }
                                if("扬程m".equals(tbtdList.get(r).text())){
                                    equipment.setLift(tbtdList.get(r+1).text());
                                }
                                if("出口压力MPa".equals(tbtdList.get(r).text())){
                                    equipment.setOutletPressure(tbtdList.get(r+1).text());
                                }
                                if("吸入温度下密度".equals(tbtdList.get(r).text())){
                                    equipment.setSuctionTemperature(tbtdList.get(r+1).text());
                                }
                            }
                            if(l==2){
                                if("驱动机名称".equals(tbtdList.get(r).text())){
                                    equipment.setDriving(tbtdList.get(r+1).text());
                                }
                                if("转速rpm".equals(tbtdList.get(r).text())){
                                    equipment.setDrivingSpeed(tbtdList.get(r+1).text());
                                }
                                if("防爆等级".equals(tbtdList.get(r).text())){
                                    equipment.setExplosionProofGrade(tbtdList.get(r+1).text());
                                }
                                if("驱动机型号".equals(tbtdList.get(r).text())){
                                    equipment.setDrivingMachine(tbtdList.get(r+1).text());
                                }
                                if("出厂日期".equals(tbtdList.get(r).text())){
                                    equipment.setDrivingProductionDate(tbtdList.get(r+1).text());
                                }
                                if("制造厂".equals(tbtdList.get(r).text())){
                                    equipment.setDrivingManufacturer(tbtdList.get(r+1).text());
                                }
                            }
                            if(l==3){

                                equipment.setEquipmentOperationParameters(tbtdList.get(r).getElementsByTag("textarea").get(0).text());
                            }
                            if(l==4){
                                if("中修检修周期".equals(tbtdList.get(r).text())){
                                    equipment.setIntermediateMaintenanceCycle(tbtdList.get(r+1).text());
                                }
                                if("大修检修周期".equals(tbtdList.get(r).text())){
                                    equipment.setOverhaulCycle(tbtdList.get(r+1).text());
                                }
                            }
                        }
                    }
                    try {
                        Elements descList = document.getElementsByClass("desc");
                        equipment.setEquipmentRenark(descList.get(0).text());
                        Elements photo = document.getElementsByClass("photo");
                        equipment.setEquipmentImg("http://www.yibiao120.com"+photo.get(0).getElementsByTag("a").get(0).attr("href"));
                        equipment.setEquipmentWorkshop("仪表车间");
                        equipment.setCreateTime(new Date());
                        nmEquipmentService.insertNmEquipment(equipment);
                    } catch (Exception e) {

                    }

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return AjaxResult.success();
    }

    /**
     * 通过正则表达式来提取内容
     * @param html 要解析的html文档内容
     * @return 解析结果，可以多次匹配，每次匹配的结果按文档中出现的先后顺序添加进结果List
     */
    public static List<String> getContext(String html, Pattern p) {
        List<String> resultList = new ArrayList();
        Matcher m = p.matcher(html);//开始编译
        while (m.find()) {
            resultList.add(m.group(0));
        }
        return resultList;
    }
}
